﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class WorkFlowArchives : YJ.Data.Interface.IWorkFlowArchives
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public WorkFlowArchives()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.WorkFlowArchives实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.WorkFlowArchives model)
        {
            string sql = @"INSERT INTO workflowarchives
				(ID,FlowID,StepID,FlowName,StepName,TaskID,GroupID,InstanceID,Title,Contents,Comments,WriteTime) 
				VALUES(@ID,@FlowID,@StepID,@FlowName,@StepName,@TaskID,@GroupID,@InstanceID,@Title,@Contents,@Comments,@WriteTime)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@FlowID", MySqlDbType.VarChar, 36){ Value = model.FlowID },
				new MySqlParameter("@StepID", MySqlDbType.VarChar, 36){ Value = model.StepID },
				new MySqlParameter("@FlowName", MySqlDbType.Text, -1){ Value = model.FlowName },
				new MySqlParameter("@StepName", MySqlDbType.Text, -1){ Value = model.StepName },
				new MySqlParameter("@TaskID", MySqlDbType.VarChar, 36){ Value = model.TaskID },
				new MySqlParameter("@GroupID", MySqlDbType.VarChar, 36){ Value = model.GroupID },
				new MySqlParameter("@InstanceID", MySqlDbType.Text, -1){ Value = model.InstanceID },
				new MySqlParameter("@Title", MySqlDbType.Text, -1){ Value = model.Title },
				new MySqlParameter("@Contents", MySqlDbType.LongText, -1){ Value = model.Contents },
				new MySqlParameter("@Comments", MySqlDbType.LongText, -1){ Value = model.Comments },
				new MySqlParameter("@WriteTime", MySqlDbType.DateTime, -1){ Value = model.WriteTime }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.WorkFlowArchives实体类</param>
        public int Update(YJ.Data.Model.WorkFlowArchives model)
        {
            string sql = @"UPDATE workflowarchives SET 
				FlowID=@FlowID,StepID=@StepID,FlowName=@FlowName,StepName=@StepName,TaskID=@TaskID,GroupID=@GroupID,InstanceID=@InstanceID,Title=@Title,Contents=@Contents,Comments=@Comments,WriteTime=@WriteTime
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@FlowID", MySqlDbType.VarChar, 36){ Value = model.FlowID },
				new MySqlParameter("@StepID", MySqlDbType.VarChar, 36){ Value = model.StepID },
				new MySqlParameter("@FlowName", MySqlDbType.Text, -1){ Value = model.FlowName },
				new MySqlParameter("@StepName", MySqlDbType.Text, -1){ Value = model.StepName },
				new MySqlParameter("@TaskID", MySqlDbType.VarChar, 36){ Value = model.TaskID },
				new MySqlParameter("@GroupID", MySqlDbType.VarChar, 36){ Value = model.GroupID },
				new MySqlParameter("@InstanceID", MySqlDbType.Text, -1){ Value = model.InstanceID },
				new MySqlParameter("@Title", MySqlDbType.Text, -1){ Value = model.Title },
				new MySqlParameter("@Contents", MySqlDbType.LongText, -1){ Value = model.Contents },
				new MySqlParameter("@Comments", MySqlDbType.LongText, -1){ Value = model.Comments },
				new MySqlParameter("@WriteTime", MySqlDbType.DateTime, -1){ Value = model.WriteTime },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM workflowarchives WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.WorkFlowArchives> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.WorkFlowArchives> List = new List<YJ.Data.Model.WorkFlowArchives>();
            YJ.Data.Model.WorkFlowArchives model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.WorkFlowArchives();
                model.ID = dataReader.GetString(0).ToGuid();
                model.FlowID = dataReader.GetString(1).ToGuid();
                model.StepID = dataReader.GetString(2).ToGuid();
                model.FlowName = dataReader.GetString(3);
                model.StepName = dataReader.GetString(4);
                model.TaskID = dataReader.GetString(5).ToGuid();
                model.GroupID = dataReader.GetString(6).ToGuid();
                model.InstanceID = dataReader.GetString(7);
                model.Title = dataReader.GetString(8);
                model.Contents = dataReader.GetString(9);
                model.Comments = dataReader.GetString(10);
                model.WriteTime = dataReader.GetDateTime(11);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.WorkFlowArchives> GetAll()
        {
            string sql = "SELECT * FROM workflowarchives";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.WorkFlowArchives> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM workflowarchives";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.WorkFlowArchives Get(Guid id)
        {
            string sql = "SELECT * FROM workflowarchives WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.WorkFlowArchives> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 得到一页数据
        /// </summary>
        /// <param name="pager"></param>
        /// <param name="query"></param>
        /// <param name="title"></param>
        /// <param name="flowIDString"></param>
        /// <returns></returns>
        public System.Data.DataTable GetPagerData(out string pager, string query = "", string title = "", string flowIDString = "")
        {
            StringBuilder WHERE = new StringBuilder();
            List<MySqlParameter> parList = new List<MySqlParameter>();
            if (!title.IsNullOrEmpty())
            {
                WHERE.Append("AND INSTR(Title,@Title)>0 ");
                parList.Add(new MySqlParameter("@Title", MySqlDbType.VarChar) { Value = title });
            }
            if (!flowIDString.IsNullOrEmpty())
            {
                WHERE.AppendFormat("AND FlowID IN({0}) ", YJ.Utility.Tools.GetSqlInString(flowIDString));
            }
            long count;
            int size = YJ.Utility.Tools.GetPageSize();
            int number = YJ.Utility.Tools.GetPageNumber();
            string sql = dbHelper.GetPaerSql("WorkFlowArchives", "*", WHERE.ToString(), "WriteTime DESC", size, number, out count, parList.ToArray());

            pager = YJ.Utility.Tools.GetPagerHtml(count, size, number, query);
            return dbHelper.GetDataTable(sql, parList.ToArray());
        }

        /// <summary>
        /// 得到一页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageNumber"></param>
        /// <param name="title"></param>
        /// <param name="flowIDString"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public System.Data.DataTable GetPagerData(out long count, int pageSize, int pageNumber, string title = "", string flowIDString = "", string date1 = "", string date2 = "", string order = "")
        {
            StringBuilder WHERE = new StringBuilder();
            List<MySqlParameter> parList = new List<MySqlParameter>();
            if (!title.IsNullOrEmpty())
            {
                WHERE.Append("AND INSTR(Title,@Title)>0 ");
                parList.Add(new MySqlParameter("@Title", MySqlDbType.VarChar) { Value = title });
            }
            if (flowIDString.IsGuid())
            {
                WHERE.Append("AND FlowID=@FlowID ");
                parList.Add(new MySqlParameter("@FlowID", flowIDString));
            }
            if (date1.IsDateTime())
            {
                WHERE.Append("AND WriteTime>=@WriteTime1 ");
                parList.Add(new MySqlParameter("@WriteTime1", date1));
            }
            if (date2.IsDateTime())
            {
                WHERE.Append("AND WriteTime<=@WriteTime2 ");
                parList.Add(new MySqlParameter("@WriteTime2", date2.ToDateTime().ToString("yyyy-MM-dd 23:59:59")));
            }

            string sql = dbHelper.GetPaerSql("WorkFlowArchives", "ID,FlowName,StepName,Title,WriteTime", WHERE.ToString(), order.IsNullOrEmpty() ? "WriteTime DESC" : order, pageSize, pageNumber, out count, parList.ToArray());
            return dbHelper.GetDataTable(sql, parList.ToArray());
        }
    }
}